﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms4
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }

        private int Id;
        public EditForm(int Id,string Name,int Age,int Score)
        {
            InitializeComponent();

            this.Id = Id;
            textBox1.Text = Name;
            textBox2.Text = Age.ToString();
            textBox3.Text = Score.ToString();
        }
        //确定按钮
        private void button1_Click(object sender, EventArgs e)
        {
            var Name = textBox1.Text;
            var Age = textBox2.Text;
            var Score = textBox3.Text;

            //修改操作
            if (this.Id > 0)
            {
                var sql = string.Format("UPDATE Students SET Name='{0}',Age={1} ,Score={2} WHERE Id={3}", Name, Age, Score, this.Id);
                DBHelper.AddUpdate(sql);
                MessageBox.Show("修改成功！！！", "消息提示");
            }
            //增加操作
            else
            {
                var sql = string.Format("INSERT INTO Students(Name,Age,Score)VALUES('{0}',{1},{2})",Name,Age,Score);
                DBHelper.AddUpdate(sql);
                MessageBox.Show("增加成功！！！","消息提示");
            }
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }
        //取消按钮
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
